package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.util.*;
import com.it.tydic.estate.module.mapper.inner.CqbgglMapper;
import com.it.tydic.estate.module.mapper.inner.FangjianguanliMapper;
import com.it.tydic.estate.module.model.inner.Cqbggl;
import com.it.tydic.estate.module.model.inner.Fangjianguanli;
import com.it.tydic.estate.module.service.CqbgglService;
import com.it.tydic.estate.module.service.FJglService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产权变更实现类
 */
@Service
@Transactional
public class CqbgglServiceImpl implements CqbgglService {
    @Autowired
    private CqbgglMapper cqbgglMapper;
    @Autowired
    private FangjianguanliMapper fangjianguanliMapper;//房间管理
    @Autowired
    private FJglService fJglService;//房间管理实现

    /**
     * 新增或修改变更产权记录
     *
     * @param cqbggl
     * @return
     */
    @Override
    public Result addOrUpload(Cqbggl cqbggl) {
        if (StringUtil.isEmpty(cqbggl.getFjid()) || StringUtil.isEmpty(cqbggl.getFjcqr())
                || StringUtil.isEmpty(cqbggl.getYycqr())) {//参数非空验证
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        String userId = CurrentUtil.getCurrentUserId();
        if (cqbggl.getId() == null || "".equals(cqbggl.getId())) {//判断是否有主键
            long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());//生成Id主键
            cqbggl.setId(String.valueOf(id));
            cqbggl.setCjsj(new Date());
            cqbggl.setZfbj(ZfbjFinalUtil.zfbj_zy);
            cqbggl.setXgsj(new Date());
            cqbggl.setBgzt("0");//添加审批状态0未审批1已审批
            cqbggl.setBgdjh(selectBGDJ());//添加单据编号
            cqbggl.setCuserId(userId);
            addFjbh(cqbggl);//添加房间编号
            int result = cqbgglMapper.insertSelective(cqbggl);
            if (result < 0)
                return Result.error(CodeMsg.INTER_ERROR);
        } else {
            cqbggl.setXgsj(new Date());
            cqbggl.setUuserId(userId);
            int result = cqbgglMapper.updateByPrimaryKeySelective(cqbggl);
            if (result < 0) {
                return Result.error(CodeMsg.UPDATE_ERROR);
            }
        }
        return Result.success(cqbggl.getId());
    }

    /**
     * 条件分页查房间变更产权记录
     *
     * @param fjglMap
     * @return
     */
    @Override
    public Result queryList(Map<String, Object> fjglMap) {
        Map<String, Object> map = new HashMap<>();
        if (fjglMap.containsKey("pageNum") && fjglMap.containsKey("pageSize")) {//判断是否有分页条件有进行分页
            PageHelper.startPage((Integer) fjglMap.get("pageNum"), (Integer) fjglMap.get("pageSize"));
        }else{
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        Page<Cqbggl> selectList = cqbgglMapper.selectList(fjglMap);
        List<Cqbggl> result = selectList.getResult();
        if (result.size() > 0) {//变更状态码值转中文
            Map<String, String> builings = fJglService.builingMap();
            for (Cqbggl cqbggl : result) {
                if ("1".equals(cqbggl.getBgzt())) {
                    cqbggl.setBgztName("是");
                } else {
                    cqbggl.setBgztName("否");
                }
                if (builings.containsKey(cqbggl.getLyid())) {//获取楼宇名称
                    cqbggl.setLyid(builings.get(cqbggl.getLyid()));
                } else {
                    cqbggl.setLyid("");
                }
            }
        }
        map.put("result", result);
        map.put("totle", selectList.getTotal());
        return Result.success(map);
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @Override
    public Result deleteBatch(List<String> list) {
        if (list == null || list.size() <= 0)
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        Example example = new Example(Cqbggl.class);
        example.createCriteria().andIn("id", list);
        Cqbggl cqbggl = new Cqbggl();
        cqbggl.setZfbj(ZfbjFinalUtil.zfbj_zf);
        int result = cqbgglMapper.updateByExampleSelective(cqbggl, example);
        if (result <= 0)
            return Result.error(CodeMsg.DELETE_ERROR);
        return Result.success();
    }

    /**
     * 根据主键查询房间变更产权记录
     *
     * @param id
     * @return
     */
    @Override
    public Result queryById(String id) {
        if (!StringUtils.isNotEmpty(id))
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        Cqbggl cqbggl = cqbgglMapper.selectByPrimaryKey(id);
        if(cqbggl==null){
            return Result.error(CodeMsg.DATA_NOT_EXIST);
        }
        return Result.success(cqbggl);
    }

    /**
     * 根据主键变更产权
     *
     * @param id
     * @return
     */
    @Override
    public Result changeById(String id) {
        if (!StringUtils.isNotEmpty(id))
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        Cqbggl cqbggl = cqbgglMapper.selectByPrimaryKey(id);//查询变更记录
        if (cqbggl != null) {//房间产权变更
            Fangjianguanli fangjianguanli = new Fangjianguanli();
            fangjianguanli.setId(cqbggl.getFjid());
            fangjianguanli.setFjcqr(cqbggl.getFjcqr());
            fangjianguanli.setSfzjh(cqbggl.getSfzjh());
            int result = fangjianguanliMapper.updateByPrimaryKeySelective(fangjianguanli);
            if (result <= 0)
                return Result.error(CodeMsg.DATA_CHANGE_FALSE);
            cqbggl.setBgsj(new Date());
            cqbggl.setBgzt("1");
            cqbgglMapper.updateByPrimaryKeySelective(cqbggl);
        } else {
            return Result.error(CodeMsg.DATA_CHANGE_FALSE);
        }
        return Result.success();
    }

    /**
     * 生成单据编号
     *
     * @return
     */
    private String selectBGDJ() {
        SimpleDateFormat simpl = new SimpleDateFormat("yyyymmddHHmmss");
        String lsh = "BGDJ-" + simpl.format(new Date());
        return lsh;
    }

    /**
     * 添加房间编号
     *
     * @param cqbggl
     */
    private void addFjbh(Cqbggl cqbggl) {
        Fangjianguanli fangjianguanli = fangjianguanliMapper.selectByPrimaryKey(cqbggl.getFjid());
        if (fangjianguanli != null) {
            cqbggl.setFjbh(fangjianguanli.getFjbh());
        } else {
            cqbggl.setFjbh("");
        }
    }
}
